/**
 * "Visual Paradigm: DO NOT MODIFY THIS FILE!"
 * 
 * This is an automatic generated file. It will be regenerated every time 
 * you generate persistence class.
 * 
 * Modifying its content may cause the program not work, or your work may lost.
 */

using System;
using Orm;
using System.Collections;
using NHibernate;

namespace petshop {
	/// <summary>
	/// ORM-Persistable Class
	/// </summary>
	public class PetFactory {
		public static global::petshop.Pet LoadPetByORMID(int id) {
			PersistentSession session = global::petshop.PetShopPersistentManager.Instance().GetSession();
			return LoadPetByORMID(session,id);
		}
		
		public static global::petshop.Pet LoadPetByORMID(PersistentSession session,int id) {
			return (global::petshop.Pet) session.Load(typeof(global::petshop.Pet), (Int32)id);
		}
		
		public static global::petshop.Pet[] ListPetByQuery(string condition, string orderBy) {
			PersistentSession session = global::petshop.PetShopPersistentManager.Instance().GetSession();
			return ListPetByQuery(session, condition, orderBy);
		}
		
		public static global::petshop.Pet[] ListPetByQuery(PersistentSession session, string condition, string orderBy) {
			global::System.Text.StringBuilder sb = new global::System.Text.StringBuilder("From petshop.Pet as Pet");
			if (condition != null) {
				sb.Append(" Where ");
				sb.Append(condition);
			}
			if (orderBy != null) {
				sb.Append(" Order By ");
				sb.Append(orderBy);
			}
			IQuery query = session.CreateQuery(sb.ToString());
			try {
				IList list = query.List();
				global::petshop.Pet[] result = new global::petshop.Pet[list.Count];
				list.CopyTo(result, 0);
				return result;
			}
			catch (Exception e) {
				global::System.Console.Error.WriteLine(e.Message);
				global::System.Console.Error.WriteLine(e.StackTrace);
				throw new PersistentException(e);
			}
		}
		
		public static global::petshop.Pet LoadPetByQuery(string condition, string orderBy) {
			PersistentSession session = global::petshop.PetShopPersistentManager.Instance().GetSession();
			return LoadPetByQuery(session, condition, orderBy);
		}
		
		public static global::petshop.Pet LoadPetByQuery(PersistentSession session, string condition, string orderBy) {
			global::petshop.Pet[] pets = ListPetByQuery(session, condition, orderBy);
			if (pets != null && pets.Length > 0)
				return pets[0];
			else
				return null;
		}
		
		public static global::System.Collections.IEnumerable IteratePetByQuery(string condition, string orderBy) {
			PersistentSession session = global::petshop.PetShopPersistentManager.Instance().GetSession();
			return IteratePetByQuery(session, condition, orderBy);
		}
		
		public static global::System.Collections.IEnumerable IteratePetByQuery(PersistentSession session, string condition, string orderBy) {
			global::System.Text.StringBuilder sb = new global::System.Text.StringBuilder("From petshop.Pet as Pet");
			if (condition != null) {
				sb.Append(" Where ");
				sb.Append(condition);
			}
			if (orderBy != null) {
				sb.Append(" Order By ");
				sb.Append(orderBy);
			}
			IQuery query = session.CreateQuery(sb.ToString());
			try {
				return query.Enumerable();
			}
			catch (Exception e) {
				global::System.Console.Error.WriteLine(e.Message);
				global::System.Console.Error.WriteLine(e.StackTrace);
				throw new PersistentException(e);
			}
		}
		
		public static Pet CreatePet() {
			return new global::petshop.Pet();
		}
		
		public Pet LoadPetByCriteria(PetCriteria petCriteria) {
			Pet[] pets = ListPetByCriteria(petCriteria);
			if(pets == null || pets.Length == 0) {
				return null;
			}
			return pets[0];
		}
		
		public Pet[] ListPetByCriteria(PetCriteria petCriteria) {
			return petCriteria.ListPet();
		}
	}
	
}
